﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibMinecraft.Server;
using LibMinecraft.Model.Packets;
using LibMinecraft.Model;

namespace PartyCraftServer.Commands
{
    public class TimeCommand : Command
    {
        public override bool RequiresOp
        {
            get { return true; }
        }

        public override string DefaultCommand
        {
            get { return "time"; }
        }

        public override string Documentation
        {
            get { return "Manipulates the current time of the server.\nUsage: /time [get|set|add] (amount)"; }
        }

        public override void ExecuteCommand(RemoteClient client, params string[] parameters)
        {
            if (parameters.Length == 0)
            {
                client.SendChat(ChatColor.Red + "Invalid parameters. /help time for usage.");
                return;
            }
            if (parameters.Length > 2)
            {
                client.SendChat(ChatColor.Red + "Invalid parameters. /help time for usage.");
                return;
            }
            long amount = 0;
            if (parameters.Length == 2)
            {
                if (!long.TryParse(parameters[1], out amount))
                {
                    client.SendChat(ChatColor.Red + "Invalid parameters. /help time for usage.");
                    return;
                }
            }

            string operation = parameters[0];
            switch (operation)
            {
                case "get":
                    if (parameters.Length == 2)
                    {
                        client.SendChat(ChatColor.Red + "Invalid parameters. /help time for usage.");
                        return;
                    }
                    client.SendChat(ChatColor.Blue + PartyCraft.Server.GetWorld(client).Time);
                    break;
                case "set":
                    PartyCraft.SendToOps(ChatColor.Blue + client.PlayerEntity.Name + " set time to " + amount.ToString());
                    PartyCraft.Server.GetWorld(client).Time = amount;
                    break;
                case "add":
                    PartyCraft.SendToOps(ChatColor.Blue + client.PlayerEntity.Name + " set time to " + (PartyCraft.Server.GetWorld(client).Time + amount).ToString());
                    PartyCraft.Server.GetWorld(client).Time += amount;
                    break;
                default:
                    client.SendChat(ChatColor.Red + "Invalid parameters. /help time for usage.");
                    break;
            }
        }
    }
}
